### Figure 1 Code for "Value Similarity and Norm Change: Null Effects and Backlash to Messaging on Same-Sex Rights in Uganda"
### Purpose: to produce map for figure 1 in main text
### Author: Nicholas Lyon


# Preliminaries -----------------------------------------------------------

# Purpose of code section: load required libraries and data

library(rio)
library(tidyverse)
library(ggplot2)
library(ggthemes)
library(spData)
library(sf)
library(countrycode)

root <- "add your path here/" # add path to where "uganda_samesex_replication" is stored
setwd(paste0(root, "uganda_samesex_replication/Output"))

status <- import(paste0(root, "uganda_samesex_replication/Data/samesex_legality.csv")) # data on legal status of same sex activity
ab <- import(paste0(root, "uganda_samesex_replication/Data/merged_r6_data_2016_36countries2.sav")) # afrobarometer round 6 data
ssa <- world %>% 
  filter(continent == "Africa", 
         name_long != "Western Sahara",
         name_long != "Morocco",
         name_long != "Algeria",
         name_long != "Tunisia",
         name_long != "Libya",
         name_long != "Egypt") # africa shapefile from spData, filtered to SSA


# Panel A: Legal Status ---------------------------------------------------

status %>%
  pivot_longer(!Year, names_to = "name_long", values_to = "status") %>%
  mutate(name_long = case_when(
    name_long == "C\x99te d'Ivoire" ~ "Côte d'Ivoire", # prep for merge
    TRUE ~ as.character(name_long)
  )) %>%
  right_join(ssa, by = "name_long") %>%
  ggplot() +
  geom_sf(aes(geometry = geom, fill = factor(status))) +
  scale_fill_manual(values = c("black", "white")) +
  theme_void() +
  theme(legend.position="bottom",
        legend.title = element_blank(),
        legend.text=element_text(size=20),
        legend.key.width = unit(2, "line")) +
  ggsave("homosexuality_legal.pdf")


# Panel B: Afrobarometer --------------------------------------------------
ab_names <- attr(ab$COUNTRY, "labels") # get country labels 

ab %>%
  mutate(name_long = factor(COUNTRY, ab_names, names(ab_names)),
         # adjust two names in afrobarometer for merge
         name_long = case_when(
           name_long == "Cote d'Ivoire" ~ "Côte d'Ivoire",
           name_long == "Swaziland" ~ "eSwatini",
           TRUE ~ as.character(name_long)
         )) %>%
  # set missing, don't know, refused, not asked in this country to missing
  mutate(homosexual_neighbor = ifelse(Q89C < 1 | Q89C > 5, NA, Q89C),
         # code as binary indicator of whether respondent would not care or like
         homosexual_binary = case_when(
           homosexual_neighbor == 1 ~ 0,
           homosexual_neighbor == 2 ~ 0,
           homosexual_neighbor == 3 ~ 1,
           homosexual_neighbor == 4 ~ 1,
           homosexual_neighbor == 5 ~ 1
         )) %>%
  group_by(name_long) %>%
  summarise(mean_homosexual_neighbor = mean(homosexual_binary, na.rm = T)) %>%
  filter(!is.na(mean_homosexual_neighbor)) %>%
  right_join(ssa, by = "name_long") %>%
  ggplot() +
  geom_sf(aes(geometry = geom, fill = mean_homosexual_neighbor)) +
  scale_fill_gradient(low = "lightgray", high = "black", 
                      na.value = "white") +
  theme_void() +
  theme(legend.position="bottom",
        legend.title = element_blank(),
        legend.text=element_text(size=20),
        legend.key.width = unit(2, "line")) + 
  ggsave("ab_homosexual_neighbor.pdf")

